package com.ruoyi.common.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.oms.entity.YytOmsOrder;
import com.ruoyi.common.oms.service.YytOmsOrderService;
import com.ruoyi.common.sms.entity.YytSmsCoupon;
import com.ruoyi.common.sms.entity.YytSmsCouponHistory;
import com.ruoyi.common.sms.dao.YytSmsCouponHistoryMapper;
import com.ruoyi.common.sms.service.YytSmsCouponHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* <p>
    * 优惠券领取历史记录 服务实现类
    * </p>
*
* @author MrPing
* @since 2022-05-31
*/
@Service
public class YytSmsCouponHistoryServiceImpl extends ServiceImpl<YytSmsCouponHistoryMapper, YytSmsCouponHistory> implements YytSmsCouponHistoryService {

    @Autowired
    private YytSmsCouponHistoryMapper yytSmsCouponHistoryMapper;

    @Autowired
    private YytOmsOrderService yytOmsOrderService;


    @Override
    public List<YytSmsCoupon> getUserCouponSpuList(String memberId, String spuId) {
        return yytSmsCouponHistoryMapper.getUserSpuHistoryList(memberId, spuId);
    }

    @Override
    public Boolean upCouponModeStatus(String ids, String orderId) {
        String[] strings = ids.split(",");
        List<String> list = new ArrayList<>();
        list.add("1569985866041880577");
        list.add("1569985596788535298");
        list.add("1569985087897825281");
        list.add("1569966862833250306");

        for (String hId : strings){
            YytSmsCouponHistory hisData = super.getById(hId);
            YytSmsCouponHistory history = new YytSmsCouponHistory();
            history.setId(hId);
            history.setUseMode(0);

            if(list.contains(hisData.getCouponId())){
                YytOmsOrder omsOrder = yytOmsOrderService.getById(orderId);
                String reStr = "|" + omsOrder.getId();
                history.setOrderList(hisData.getOrderList().replace(reStr, ""));
                history.setBalance(hisData.getBalance().add(omsOrder.getCouponAmount()));
            }
            yytSmsCouponHistoryMapper.updateById(history);

        }
        return true;
    }

    @Override
    public void getUpdateCoupon(YytOmsOrder yytOmsOrder) {
        String[] strings = yytOmsOrder.getCouponId().split(",");
        for (String ids : strings){
            YytSmsCouponHistory history = super.getById(ids);
            List<String> list = new ArrayList<>();
            list.add("1569985866041880577");
            list.add("1569985596788535298");
            list.add("1569985087897825281");
            list.add("1569966862833250306");
            if(StringUtils.isNotBlank(history.getOrderList()) && history.getOrderList().contains(yytOmsOrder.getId())){
                break;
            }
            String newOrderList = history.getOrderList() + "|" + yytOmsOrder.getId();
            if(list.contains(history.getCouponId())){
                UpdateWrapper<YytSmsCouponHistory> wrapper = new UpdateWrapper<>();
                wrapper.eq("id", history.getId())
                        .set("order_list", newOrderList);

                if (history.getBalance().compareTo(yytOmsOrder.getCouponAmount()) < 1){
                    wrapper.set("balance", 0).set("use_mode", 1);
                }else{
                    wrapper.set("balance", history.getBalance().subtract(yytOmsOrder.getCouponAmount()));
                }
                super.update(wrapper);
            }else{
                UpdateWrapper<YytSmsCouponHistory> wrapper = new UpdateWrapper<>();
                wrapper.eq("id", ids).set("use_mode", 1);
                super.update(wrapper);
            }
        }
    }
}
